<?php

namespace app\model;

use app\Api;
use think\Db;
use think\Model;

class Common extends Api
{
    protected $db_name;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 添加一条记录
     * @param $arr_data
     */
    public function toAdd($arr_data)
    {
        $arr_data['gmt_create'] = $this->_now;
        $arr_data['gmt_modified'] = $this->_now;
        Db::name($this->db_name)->insert($arr_data);
        return Db::name($this->db_name)->getLastInsID();
    }

    /**
     * 修改一条记录
     * @param $arr_where
     * @param $arr_data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function toUpdate($arr_where, $arr_data)
    {
        $arr_data['gmt_modified'] = $this->_now;
        return Db::name($this->db_name)->where($arr_where)->update($arr_data);
    }

    /**
     * 查询一条记录
     * @param $arr_data
     * @param string $str_field
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function toFind($arr_data, $str_field = '*', $order = null)
    {
        return Db::table($this->db_name)->where($arr_data)->field($str_field)->order($order)->find();
    }

    /**
     * 查询数据（多条）
     * @param $where
     * @param string $field
     * @param null $firstRow
     * @param null $listRows
     * @param null $order
     * @param null $group
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function toSelect($where, $field = '*', $firstRow = null, $listRows = null, $order = null, $group = null)
    {
        return Db::name($this->db_name)->where($where)->field($field)->limit($firstRow, $listRows)->order($order)->group($group)->select();
    }

    /**
     * 删除一条记录
     * @param $arr_where
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function toDelete($arr_where)
    {
        return Db::name($this->db_name)->where($arr_where)->delete();
    }

    /**
     * 查询和
     * @param $where
     * @param $field
     * @return float|int
     */
    public function toSum($arr_where, $str_field)
    {
        return Db::name($this->db_name)
            ->where($arr_where)
            ->sum($str_field);
    }

    /**
     * 查询条数
     * @param $where
     * @return int|string
     */
    public function toCount($arr_where)
    {
        return Db::name($this->db_name)
            ->where($arr_where)
            ->count();
    }

    /**
     * 查询某个数据值
     * @param $where
     * @param $filed
     * @return mixed
     */
    public function toValue($arr_where, $str_field, $str_order_by = false)
    {
        return Db::name($this->db_name)
            ->where($arr_where)
            ->order($str_order_by)
            ->value($str_field);
    }

    /**
     * 查询某列数据值
     * @param $where
     * @param $field
     * @return array
     */
    public function toColumn($arr_where, $str_field, $str_key = false, $order = false, $group = false)
    {
        return Db::name($this->db_name)
            ->where($arr_where)
            ->order($order)
            ->group($group)
            ->column($str_field, $str_key);
    }

    /**
     * 减
     * @param $arr_where
     * @param $str_field
     * @param int $int_num
     * @return int|true
     * @throws \think\Exception
     */
    public function valueDec($arr_where, $str_field, $int_num = 1)
    {
        return Db::name($this->db_name)
            ->where($arr_where)
            ->setDec($str_field, $int_num);
    }

    /**
     * 加
     * @param $arr_where
     * @param $str_field
     * @param int $int_num
     * @return int|true
     * @throws \think\Exception
     */
    public function valueInc($arr_where, $str_field, $int_num = 1)
    {
        return Db::name($this->db_name)
            ->where($arr_where)
            ->setInc($str_field, $int_num);
    }
}
